# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

use strict;
use warnings;

package CCNCore::Model::User;

use Jifty::DBI::Schema;

use CCNCore::Record schema {

    column web_group =>
        is mandatory,
        is indexed,
        references CCNCore::Model::Groups,
        since '0.0.1',
        label is 'Primary Group',
        hints is 'The primary group the person belongs to';

    column primary_language =>
        type is 'text',
        since '0.0.1',
        label is 'Language',
        hints is 'The primary language of this person. (Currently unused.)';
    
        
};

use Jifty::Plugin::User::Mixin::Model::User;
use Jifty::Plugin::Authentication::Password::Mixin::Model::User;
use CCNCore::Mixin::Active;

# Your model-specific methods go here.

sub since { 0.0.1 }
sub menu { 'User Management' }

sub current_user_can 
{
     my $self = shift;
     my $type = shift;
     my %args = (@_);

     # Prevent regular users to modify their primary group.
     return 0 
         if ( $type eq 'update'
             and !$self->current_user->is_superuser
             and $args{'column'} eq 'web_group' ); 

     return 1;
}

1;

__END__
=pod

 CREATE TABLE "ui_user" (
     -- #compute SELECT CASE COUNT(*) WHEN 0 THEN 1 ELSE MAX(pk)+1 END AS pk FROM ui_user;
     "pk"                INTEGER NOT NULL PRIMARY KEY,               -- @pk          "hidden"    aka ui_user_pk
 
     -- MySQL needs a length (not TEXT) for UNIQUE/keys
     "login"             VARCHAR(64) NOT NULL,                       --  @login      "Login"     
     -- attributes
     "label"             TEXT NOT NULL,                              --  @string     "Name"
     "web_group"         INTEGER NOT NULL REFERENCES ui_group(pk),   --  @pk         "Primary group"
     "password"          TEXT NOT NULL,                              --  @password   "Password" dontlist
     "language"          TEXT,                                       --  @language   "Language"
 
     "person_pk"         INTEGER         REFERENCES _persons(pk),    --  @pk         "Person"
 
     -- Current session
     "session_id"        TEXT,                                       --  @string "hidden"    dontlist nomodify
     "session_start"     TIMESTAMP,                                  --  @string "hidden"    dontlist nomodify
 
     UNIQUE (web_group,login)
 ) _{OPTIONS_UTF8}_;
 
 INSERT INTO ui_user(pk,web_group,login,password,label) VALUES (0,0,'admin','changeme','Administrator');

=cut

=pod
    column login =>
        type is 'varchar(64)',
        max_length is 64,
        is mandatory,
        is indexed,
        validator is \&CCN::Framework::Type::login::validator,
        label is 'Login',
        hints is 'The login for this person.';
    
    # XXX How do we express that (login,web_group) should be unique?

    column full_name =>
        type is 'text', 
        is mandatory,
        filters are 'Jifty::DBI::Filter::utf8',
        label is 'Full Name',
        hints is 'The full name for this person.';

    column password =>
        type is 'text',
        is mandatory,
        is unreadable,
        max_length is 255,
        label is 'Password',
        hints is 'The password this person uses to access the system';

    column session_id =>
        type is 'text',
        is unreadable,
        label is 'Session Identifier';
    
    column session_start =>
        type is 'timestamp',
        is unreadable,
        label is 'Session Start Time';

=cut
